package com.ymm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ymm.domain.entity.Role;
import com.ymm.domain.entity.User;
import com.ymm.domain.entity.UserRole;
import com.ymm.mapper.UserRoleMapper;
import com.ymm.service.RoleService;
import com.ymm.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户和角色关联表(UserRole)表服务实现类
 *
 * @author 椰咩咩咩
 * @since 2023-04-03 04:16:23
 */
@Service("userRoleService")
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {

    @Autowired
    private RoleService roleService;

    @Override
    public List<Long> getRIdByUserId(Long id) {

        LambdaQueryWrapper<UserRole> qw = new LambdaQueryWrapper<>();
        qw.eq(UserRole::getUserId, id);
        List<UserRole> userRoles = list(qw);
        //获取Role的ID列表
        List<Long> roleIds = userRoles.stream()
                .map(userRole -> userRole.getRoleId())
                .collect(Collectors.toList());
        return roleIds;
    }

    @Override
    public void deleteRoleByUser(UserRole userRole) {
        LambdaUpdateWrapper<UserRole> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserRole::getUserId, userRole.getUserId());
        wrapper.eq(UserRole::getRoleId, userRole.getRoleId());
        remove(wrapper);
    }
}

